﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using System.ServiceModel.Security;

namespace XRMServiceLibrary
{
    /// <summary>
    /// ExceptionType
    /// </summary>
    public enum ExceptionType
    {
        OrganizationServiceFault,
        TimeoutException,
        Exception,
        ExpiredSecurityTokenException,
        SecurityAccessDeniedException,
        MessageSecurityException,
        SecurityNegotiationException
    }

    /// <summary>
    /// BaseLogger
    /// </summary>
    public class BaseLogger : baseCRMService
    {

        public static string EmailSubject = "Email Subject";
        public static string MailTO;
        public static string MailCC;
        public static string MailBcc;
        public static string MailFrom;

        public static string _defaultMailTO = "your email id";
        public static string _defaultMailCC = "your email id";

        /// <summary>
        /// LogError
        /// </summary>
        /// <param name="ex"></param>
        public static void LogError(Object ex, int ExcType, Int32 LogType = (int)ErrorlogType.EmailLog)
        {
            switch (ExcType)
            {
                case (Int16)ExceptionType.OrganizationServiceFault:
                    {
                        if ((Int32)LogType == (Int32)ErrorlogType.WriteLog)
                        {
                            ErrorLogger.LogError((FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.EmailLog)
                        {
                            EmailLogger.LogError((FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.Both)
                        {
                            ErrorLogger.LogError((FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)ex);
                            EmailLogger.LogError((FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)ex);
                        }
                        break;
                    }
                case (Int16)ExceptionType.TimeoutException:
                    {
                        if ((Int32)LogType == (Int32)ErrorlogType.WriteLog)
                        {
                            ErrorLogger.LogError((TimeoutException)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.EmailLog)
                        {
                            EmailLogger.LogError((TimeoutException)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.Both)
                        {
                            ErrorLogger.LogError((TimeoutException)ex);
                            EmailLogger.LogError((TimeoutException)ex);
                        }
                        break;
                    }
                case (Int16)ExceptionType.Exception:
                    {
                        if ((Int32)LogType == (Int32)ErrorlogType.WriteLog)
                        {
                            ErrorLogger.LogError((Exception)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.EmailLog)
                        {
                            EmailLogger.LogError((Exception)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.Both)
                        {
                            ErrorLogger.LogError((Exception)ex);
                            EmailLogger.LogError((Exception)ex);
                        }
                        break;
                    }
                case (Int16)ExceptionType.ExpiredSecurityTokenException:
                    {
                        if ((Int32)LogType == (Int32)ErrorlogType.WriteLog)
                        {
                            ErrorLogger.LogError((ExpiredSecurityTokenException)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.EmailLog)
                        {
                            EmailLogger.LogError((ExpiredSecurityTokenException)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.Both)
                        {
                            ErrorLogger.LogError((ExpiredSecurityTokenException)ex);
                            EmailLogger.LogError((ExpiredSecurityTokenException)ex);
                        }
                        break;
                    }
                case (Int16)ExceptionType.SecurityAccessDeniedException:
                    {
                        if ((Int32)LogType == (Int32)ErrorlogType.WriteLog)
                        {
                            ErrorLogger.LogError((SecurityAccessDeniedException)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.EmailLog)
                        {
                            EmailLogger.LogError((SecurityAccessDeniedException)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.Both)
                        {
                            ErrorLogger.LogError((SecurityAccessDeniedException)ex);
                            EmailLogger.LogError((SecurityAccessDeniedException)ex);
                        }
                        break;
                    }
                case (Int16)ExceptionType.MessageSecurityException:
                    {
                        if ((Int32)LogType == (Int32)ErrorlogType.WriteLog)
                        {
                            ErrorLogger.LogError((MessageSecurityException)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.EmailLog)
                        {
                            EmailLogger.LogError((MessageSecurityException)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.Both)
                        {
                            ErrorLogger.LogError((MessageSecurityException)ex);
                            EmailLogger.LogError((MessageSecurityException)ex);
                        }
                        break;
                    }
                case (Int16)ExceptionType.SecurityNegotiationException:
                    {
                        if ((Int32)LogType == (Int32)ErrorlogType.WriteLog)
                        {
                            ErrorLogger.LogError((SecurityNegotiationException)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.EmailLog)
                        {
                            EmailLogger.LogError((SecurityNegotiationException)ex);
                        }
                        else if ((Int32)LogType == (Int32)ErrorlogType.Both)
                        {
                            ErrorLogger.LogError((SecurityNegotiationException)ex);
                            EmailLogger.LogError((SecurityNegotiationException)ex);
                        }
                        break;
                    }
            }
        }
    }
}
